package org.csu.mypetstore.controller;


import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.impl.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
@SessionAttributes({"allOrderList","order","orderList"})
public class OrderController {
    @Autowired
    private OrderService orderService;


    //查询所有订单
    @GetMapping("/orderControl")
    public String orderControl(Model model){
        List<Order> orderList = orderService.getOrdersList();
        model.addAttribute("allOrderList",orderList);
        return "Order/order_info";
    }
    //进入更新订单信息的表格
    @GetMapping("/updateOrder")
    public String updateOrder(Model model,int orderId){
        Order order =orderService.getOrder(orderId);
        model.addAttribute("order",order);
        return "Order/order_mdf";
    }
    //订单更新提交按钮
    @PostMapping("/myUpdate")
    public String myUpdate(Order order, Model model, HttpServletRequest req){
//        Date orderDate=(Date)req.getParameter("orderDate");
        String username = req.getParameter("username");
        String cardType =req.getParameter("cardType");
        String creditCard=req.getParameter("creditCard");
        String expiryDate=req.getParameter("expiryDate");
        System.out.println("修改的用户名是"+username);
//        order.setOrderDate(orderDate);

        orderService.updateOrder(order);
        List<Order> orderList=orderService.getOrdersList();
        model.addAttribute("ordermsg","订单修改信息成功!");
        model.addAttribute("allOrderList",orderList);
        return "Order/order_mdf";
    }
    //发货信息管理
    @GetMapping("/ordershipping")
    public String orderShipping(Model model){
        List<Order> orderList = orderService.getOrdersList();
        model.addAttribute("allOrderList",orderList);
        return "Order/order_shipping";

    }

    //点击发货按钮发货
    @GetMapping("/deliver")
    public String deliver(String orderId,Model model) {
        int orderId1 = Integer.parseInt(orderId);
        Order order = orderService.getOrder(orderId1);
        if(order.getStatus().equals("delivered") ){
            order.setStatus("not delivered");
            System.out.println("database has been changed to not delivered");
            //更新商家库存
            for (int i = 0; i < order.getLineItems().size(); i++) {
                String itemId = order.getLineItems().get(i).getItemId();
                System.out.println("item is "+itemId);
                int qtyLose = order.getLineItems().get(i).getQuantity();
                System.out.println("buying qty is "+qtyLose);
//                int qtyNow = catalogService.getQTYFromInventory(itemId);
//                System.out.println("now qty is "+qtyNow);
//                catalogService.updateInventory(itemId,qtyNow+qtyLose);
            }
        }
        else{
            order.setStatus("delivered");
            System.out.println("database has been changed to delivered");
            //更新商家库存
            for (int i = 0; i < order.getLineItems().size(); i++) {
                String itemId = order.getLineItems().get(i).getItemId();
                System.out.println("item is "+itemId);
                int qtyLose = order.getLineItems().get(i).getQuantity();
                System.out.println("buying qty is "+qtyLose);
//                int qtyNow = catalogService.getQTYFromInventory(itemId);
//                System.out.println("now qty is "+qtyNow);
//                catalogService.updateInventory(itemId,qtyNow-qtyLose);
            }
        }
        System.out.println("orderstatus："+order.getStatus());
        orderService.updateOrder(order);
        List<Order> orderList = orderService.getOrdersList();
        model.addAttribute("allOrderList",orderList);
        return "Order/order_shipping";
    }
    @GetMapping("/removeOrder")
    public String deleteOrder(Model model,int orderId){
        System.out.println("要删除的订单信息是："+orderId);
        orderService.removeOrder(orderId);
        List<Order> orderList = orderService.getOrdersList();
        model.addAttribute("allOrderList",orderList);
        return "Order/order_info";
    }
    //搜索订单编号
    @PostMapping("/searchOrderList")
    public String searchOrderList(Model model,String keyword){
        System.out.println("查询关键字："+keyword);
        List<Order> orderList = orderService.searchOrderList(keyword);
        model.addAttribute("allOrderList",orderList);
        return "Order/order_info";

    }





}
